package com.shujia.spark

import java.lang

import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}

object Demo24Acc {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
      .setMaster("local")
      .setAppName("Demo23Cache")

    val sc = new SparkContext(conf)

    val studentsRDD: RDD[String] = sc.textFile("data/students.txt")

    var count = 0

    studentsRDD.foreach(
      student => {
        //在算子内修改算子外的一个变量不会生效
        count += 1
        println(count)
        println(student)
      }
    )

    println(s"count:$count")

    /**
      * 累加器
      *
      */
    //1、在Driver端定义一个累加器
    val accCount: LongAccumulator = sc.longAccumulator


    studentsRDD.foreach(student => {
      //2、在Executor端对累加器进行累加计算
      accCount.add(1)
    })

    //3、在Driver端读取累加结果
    val studentCount: Long = accCount.value

    println(s"studentCount：$studentCount")


  }

}
